package cn.dankal.basiclib.common.upload.ali;


import android.content.Context;

import com.alibaba.sdk.android.oss.ClientConfiguration;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.common.OSSLog;
import com.alibaba.sdk.android.oss.common.auth.OSSAuthCredentialsProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;

import cn.dankal.basiclib.common.qiniu.UploadListener;
import cn.dankal.basiclib.common.upload.IUpload;
import cn.dankal.basiclib.util.StringUtil;

import static cn.dankal.basiclib.common.upload.Config.TYPE_IMG;


/**
 * Created by Fred on 2016/10/19.
 */

public class AliUploadHelper implements IUpload {
    private  Context context;
    private int RESULT_LOAD_IMAGE = 1;
    private AliUIDisplayer mUIDisplayer;
    private String objectName;

    //OSS的上传下载
    private AliOssService mService;
    public AliUploadHelper(Context context,AliUIDisplayer mUIDisplayer) {
        this.context=context;
        this.mUIDisplayer=mUIDisplayer;
        init();
    }


    @Override
    public void init() {

//        移动端是不安全环境，不建议直接使用阿里云主账号ak，sk的方式。建议使用STS方式。具体参
//        https://help.aliyun.com/document_detail/31920.html
//        注意：SDK 提供的 PlainTextAKSKCredentialProvider 只建议在测试环境或者用户可以保证阿里云主账号AK，SK安全的前提下使用。具体使用如下
//        主账户使用方式
//        String AK = "******";
//        String SK = "******";
//        credentialProvider = new PlainTextAKSKCredentialProvider(AK,SK)
//        以下是使用STS Sever方式。
//        如果用STS鉴权模式，推荐使用OSSAuthCredentialProvider方式直接访问鉴权应用服务器，token过期后可以自动更新。
//        详见：https://help.aliyun.com/document_detail/31920.html
//        OSSClient的生命周期和应用程序的生命周期保持一致即可。在应用程序启动时创建一个ossClient，在应用程序结束时销毁即可。

        //使用自己的获取STSToken的类
        OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(Config.STS_SERVER_URL);
        ClientConfiguration conf = new ClientConfiguration();
        conf.setConnectionTimeout(15 * 1000); // 连接超时，默认15秒
        conf.setSocketTimeout(15 * 1000); // socket超时，默认15秒
        conf.setMaxConcurrentRequest(9); // 最大并发请求书，默认5个
        conf.setMaxErrorRetry(2); // 失败后最大重试次数，默认2次
        OSS oss = new OSSClient(context.getApplicationContext(), Config.OSS_ENDPOINT, credentialProvider, conf);
        OSSLog.enableLog();
        mService= new AliOssService(oss, Config.BUCKET_NAME,mUIDisplayer);
    }

    public void uploadPic(UploadListener uploadListener, String localPath) {
        if (!StringUtil.isValid(localPath)) {
            uploadListener.onError("图片地址不为空");
            return;
        }
        objectName = "fg_" + System.currentTimeMillis()+".png";
        mService.asyncPutData(objectName, localPath);
    }

    public void uploadVideo(UploadListener uploadListener, String localPath) {
        if (!StringUtil.isValid(localPath)) {
            uploadListener.onError("视频地址不为空");
            return;
        }
        objectName = "fg_" + System.currentTimeMillis()+".mp4";
        mService.asyncPutData(objectName, localPath);
    }

    @Override
    public void multyUpload() {

    }

    @Override
    public void upload(UploadListener uploadListener, String localPath) {
        if (!StringUtil.isValid(localPath)) {
            uploadListener.onError("地址不为空");
            return;
        }


    }

    @Override
    public void upload(UploadListener uploadListener, String localPath, String type) {
        if (type.equals(TYPE_IMG)){
            uploadPic(uploadListener,localPath);
        }else {
            uploadVideo(uploadListener,localPath);
        }
    }

    public void cancle() {
//        mService.
    }

}
